﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta name="format-detection" content="telephone=no" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
    <title>首页</title>
    <link rel="stylesheet" href="css/style.css?v=1.11">
    <link rel="stylesheet" href="css/index.css?v=1.11">
    <script src="js/jquery-1.8.3.min.js?v=1.11"></script>
    <script src="js/flexible.js?v=1.11"></script>
    <script src="layer-mobile/layer.js?v=1.11"></script>
</head>
	<body class="index-body">
        <div class="home-top">
            <div class="yun-drill"><img src="image/hashrate.png" />当前算力：<span class="yun-num">0</span> <a href="javascript:;" class="rule">空投规则</a></div>
            <div class="meg-view">
                <ul class="meg-list">
                    <li></li>
                </ul>
            </div>
            <div class="count-down">
                
            </div>
            <div class="token">
               
            </div>
            <div class="wallter-btn"> <a href="property.html" id="wallter"> <img src="image/wallet.png" class="pic-img" /> 我的资产</a> </div>
            <div class="invite-btn"> <a href="invite.html" id="invite"><img src="image/invite.png" class="pic-img" /> 邀请好友</a> </div>
        </div>
		
        <div class="index-list">
          
        </div>
		<footer class="footer">
			<a class="space" id="wrill">
				<img src="image/base-h.png" class="space-img">
				<span class="space-color">空投</span>
			</a>
			
			<a href="user.html" class="space" id="user">
				<img src="image/personal.png" class="space-img">
				<span>我的</span>
			</a>
		</footer>
	</body>
</html>
<script type="text/javascript">
$(function () {
    var timers = null;
    var userid = getUserId();//获取userid
    var windowHeight = $(window).height();
    $('.index-body').css("height", windowHeight);
    init();
    function init() {

        $ajax(
            url + 'getHomeInfo.ashx',
            'get',
            { userid: userid },
            function (data) {

                var res = '',
                    advres = '',
                    message = data.data.announcement,//公告集合
                    adver = data.data.advertList,//空投项目预约列表
                    credits = data.data.currentChargeCredits;//可收集云钻列表

                if (adver.length > 0) { //显示空投

                    for (var i = 0, iL = adver.length; i < iL; i++) {
                        advres += '<a href=' + adver[i].advertPageUrl + ' class="adv"><img src='+ adver[i].advertPicUrl + '></a>'
                    }

                    $('.index-list').html(advres);

                    var adWidth = $('.index-list a').width() * adver.length + 20 * (adver.length + 2);//计算广告总长度
                    $('.index-list').css('width', adWidth);


                    $('.index-list').on('touchstart', function (e) {
                        
                        startX = e.originalEvent.changedTouches[0].pageX,
                        tranX = startX - $(this).offset().left;
                    });
                    $('.index-list').on('touchmove', function (e) {
                        
                        moveEndX = e.originalEvent.changedTouches[0].pageX,
                        X = moveEndX - startX;
                        var marLeft = e.originalEvent.changedTouches[0].pageX - tranX;

                        if ($('.index-list').width() > $(window).width()) { //广告大于三张才会滑动（总宽度大于屏幕宽度才会滑动）
                            if (X > 0) {

                                if (marLeft >= 10) {
                                    marLeft = 10
                                }

                            } else if (X < 0) {

                                if (marLeft < $(window).width() - $('.index-list').width()) {
                                    marLeft = $(window).width() - $('.index-list').width()
                                }
                            }
                            $(this).css("marginLeft", marLeft);
                        }
                    })

                }
               
                if (message.length > 0) {

                    for (var i = 0, iL = message.length; i < iL; i++) {

                        res += '<a href="javascript:;">' + message[i].title + '</a>';

                    }
                    $('.meg-list li').append(res);

                    var _width = $('.meg-list li').width();//公告的总宽度
                    var css = { left: -_width + 300 };
                    $('.meg-list li').animate(css, _width * 15, 'linear', rowBack);

                    function rowBack() {

                        $('.meg-list li').css('left', 0);
                        $('.meg-list li').animate(css, _width * 15, 'linear', rowBack);

                    }

                }

                if (userid == null) {//未登录

                    $('.count-down').html('<a href="/register.html" class="count-time"><img src="image/bubble-win.png" ></a><p><a href="/login.html">登录</a> / <a href="/register.html">注册</a></p>');

                } else { //已登录

                    $('.yun-num').html(data.data.knowledge);//显示算力

                    if (credits.length > 0) {//有钻待收

                        $('.token').css('z-index', '0');
                        $('.count-down').empty(); //清除倒计时

                        var strB = '';
                        for (var i = 0, iL = credits.length; i < iL; i++) {

                            strB += '<p class="b-wrill">' +
                                '<span class="b-pic" style="color:' + credits[i].colorHex + '" data-id="' + credits[i].id + '">' + credits[i].currencyCode + '</span>' +
                                '<span class="b-num" style="color:' + credits[i].colorHex + '">' + credits[i].value + '</span>' +
                                '</p>';
                        }

                        $('.token').html(strB);

                        //云钻上下抖动
                        var class_top = { marginTop: '10px' };
                        $('.b-wrill').animate(class_top, 1000, 'linear', topBack);
                        function topBack() {

                            if (class_top.marginTop == '10px') {
                            	class_top.marginTop = '-10px';
                            } else if (class_top.marginTop == '-10px') {
                            	class_top.marginTop = '10px';
                            }
                            $('.b-wrill').animate(class_top, 1000, 'linear', topBack);
                        } 
                        for (var i = 0, iL = credits.length; i < iL; i++) {
                            
                            //生成云钻随机分布
                            $('.b-wrill').eq(i).css({
                                left: radomNu(0.5, 8.2) + 'rem',
                                top: radomNu(0.5, 7.5) + 'rem'
                            });

                            for (var k = 0; k <= i-1; k++) {                              
                                 
                                if ((Math.abs(parseInt($('.b-wrill').eq(i).css('left')) - parseInt($('.b-wrill').eq(k).css('left')))) < 80 && (Math.abs(parseInt($('.b-wrill').eq(i).css('top')) - parseInt($('.b-wrill').eq(k).css('top')))) < 115) {

                                    $('.b-wrill').eq(i).css({
                                        left: radomNu(0.5, 8.2) + 'rem',
                                        top: radomNu(0.5, 7.3) + 'rem'
                                    });

                                } 
                            }

                        }

                    } else {//还未生成钻

                        $('.count-down').html('<span class="count-time"><i id="time"></i> 后可收取</span><img src="image/bubble-win.png" ><p>正在生长中</p>')

                        countDown(data.data.chargeTime, $('#time').get(0), function () {
                           
                            if ($('#time').text() == '0:0:0') {
                               
                                setTimeout(init, 1000);//1秒后重新加载ajax
                            }
                        });
                       
                    }

                }

            },
            function (data) {

                loginRes();
            }
        )

    };
    
	var creditid;
	$('.token').click(function(e){//事件委托
		
		e = e || window.event;
        var target = e.target || e.srcelement;//兼容ie

		if(target.parentNode.tagName.toLowerCase() == 'p'){ 
            getAudio();//收钻音频;
			creditid = $(target.parentNode).find('.b-pic').attr('data-id'); //获取creditid
			
			$ajax(
				url+'chargeCredits.ashx',
				'post',
				{
					userid:userid,
					creditid:creditid
				},
				function(data){
                    //点击效果
					doMove($(target.parentNode)[0],'top',10,-500,function(){ 
                        $(target.parentNode)[0].remove();//上升后删除节点

                        if (!$('.token').children()[0]) {
                            init();
                        }
                        
					});
					
				},
				function(data){
					
					layer.open({
						content: data.msg,
						skin: 'msg',
						time: 2
					});
					
					if(data.code == 201 || data.code == 202){
                        loginRes();
					}
					
				}
			)
		}
		
	})
	
})	
</script>